﻿var express = require('express');
var router = express.Router();

//var mongoose = require('mongoose');
var user = require('../../app_modules/dbAccess/Regist_Mongo.js');

router.get('/regist', function (req, res, next) {
    console.log("regist");
    res.render('user/regist', { title: 'Express' });
});

router.post('/add', function (req, res) {
    try{console.log("add");
    var userObj = new user();
    userObj.add({
        loginName: req.body.loginName,
        userName: req.body.userName,
        password: req.body.password1,
        telephoe: req.body.telephoe,
        email: req.body.email,
        qq: req.body.qq,
        weixin: req.body.weixin,
        babyBirthday: req.body.babyBirthday,
        babySex: req.body.babySex,
    }, function (err, rs) {
        if (err) {
            console.log(err);
            res.json({ success: false });
        } else {
            res.json({ success: true });
        }

    });    }catch(ex){}
});


router.post('/exist', function (req, res) {
    try{console.log("exist");
    eval("var re = /" + req.body.loginName + "/i;");
    var resultObj = user.find({ "loginName":  re }, function (err, result) {
        if (err) {
            res.json({state:"error"});
        } else {
            if (result.length >0) {
                res.json({ state: "true" });
            }
            else {
                res.json({ state: "false" });
            }
        }
    });}catch(ex){}
});


router.post('/getList', function (req, res) {
    try{console.log("getList");
    var resultObj = user.find(function (err, result) {
        if (err) {
            res.json({ state: "error" });
        } else {
            if (result.length > 0) {
                res.json(result);
            }
            else {
                res.json({ state: "false" });
            }
        }
    });}catch(ex){}
});


router.post('/login', function (req, res) {
    try{console.log("login");
    if (req.session.user) {
        delete req.session.user;
        console.log("delete session.");
    }
    eval("var re = /" + req.body.loginName + "/i;");
    var resultObj = user.find({ "loginName": re,"password":req.body.password }, function (err, result) {
        if (err) {
            res.json({ state: "error" });
        } else {           
            if (result.length > 0) {
                if (req.session.user) delete req.session.user;

                req.session.user = result[0];                
                res.json({ state: "true", loginName: result[0].loginName, userName: result[0].userName });
            }
            else {
                res.json({ state: "false" });
            }
        }
    });}catch(ex){}
});

router.post('/loginOut', function (req, res) {
    try {
        if (req.session.user)
            delete req.session.user;
        console.log("delete session");
        res.json({ state: "success" });
    }
    catch (e) {
        console.log("error:" + e.message.toString());
        res.json({ state: "error" });
    }
});

router.post('/GetLoginer', function (req, res) {
    try {
        if (req.session.user) {
            console.log("have session");
            console.dir(req.session.user);
            res.json({ state: "true", result: req.session.user });
        } else {
            console.log("no session");
            res.json({ state: "false" });
        }
    }
    catch (e) {
        console.log("error:" + e.message.toString());
        res.json({ state: "error" });
    }
});

module.exports = router;